package edu.snake.implement.view;

import edu.snake.core.model.Character;
import edu.snake.core.model.Position;
import edu.snake.implement.ConfigConstant;

import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Deque;


public class SnakeRender {
    Image head;
    Image body;
    Image tail;

    public SnakeRender() {
        this.head = new ImageIcon(this.getClass().getResource("head.png")).getImage();
        this.body = new ImageIcon(this.getClass().getResource("dot.png")).getImage();
        this.tail = new ImageIcon(this.getClass().getResource("tail.png")).getImage();
    }

    public void render(Position position, Character character, Graphics g) {
        Deque<Character> characterList = character.getAllElement();
        Image image = body;
        if (characterList!=null && characterList.size() > 0) {
            if (characterList.getLast().getPosition().equals(position)) {
                image = head;
                draw(position, g, image);
            }else if (characterList.getFirst().getPosition().equals(position)) {
                image = tail;
                draw(position, g, image);

            } else {
                draw(position, g, image);
            }

        }
    }


    public void draw(Position position, Graphics g, Image image) {
        int cellSizePixel = ConfigConstant.cellSizePixel;

        g.fillRect(
                position.getWidth() * cellSizePixel
                , position.getHeight() * cellSizePixel
                , cellSizePixel
                , cellSizePixel
        );
        g.setColor(Color.LIGHT_GRAY);
        g.drawRect(
                position.getWidth() * cellSizePixel
                , position.getHeight() * cellSizePixel
                , cellSizePixel
                , cellSizePixel
        );

        g.drawImage(image
                , position.getWidth() * cellSizePixel
                , position.getHeight() * cellSizePixel
                , cellSizePixel
                , cellSizePixel
                , null);

    }
}